package com.emperor.customer.app;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.hibernate.Session;

import com.emperor.cashier.app.CashierAppCtrl;
import com.emperor.core.app.App;
import com.emperor.core.app.AppBase;
import com.emperor.core.app.AppConstant;
import com.emperor.customer.logic.Customer;
import com.emperor.customer.logic.CustomerModel;
import com.emperor.util.DateFormatter;
import com.emperor.util.HibernateUtil;
import com.swtdesigner.SWTResourceManager;

public class CustomerAppMgm extends AppBase implements App {

	private Button pickDateButton_2;
	private Button pickDateButton_1;
	private Button pickDateButton;
	private Text text_11;
	private Text text_10;
	private Text text_9;
	private Text text_8;
	private Text text_7;
	private Text text_6;
	private Text text_5;
	private Text text_4;
	private Text text_3;
	private Text text_2;
	private Text text_1;
	private Text text;
	private Combo combo_3;
	private Text text_16;
	private Combo combo_2;
	private Text text_15;
	private Text text_14;
	private Combo combo_1;
	private org.eclipse.swt.widgets.List list;
	private Text text_13;
	protected Shell shell;
	public static Logger log = Logger.getLogger(CustomerAppMgm.class);
	public SimpleDateFormat sdf = new SimpleDateFormat(DateFormatter.YYYYMMDD);
	
	public CustomerAppMgm() {
		super();
	}

	public CustomerAppMgm(Map<String, Object> params) {
		super();
		this.params = params;
	}

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CustomerAppMgm window = new CustomerAppMgm();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackgroundImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/main.jpg"));
		shell.setImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/iMac OSX.png"));
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setSize(1280, 800);
		shell.setText("Update Customer");

		initScreen(shell, getParams());

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(10, 50, 872, 599);
		
		final Button updateButton = new Button(shell, SWT.NONE);
		updateButton.setImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/Edit.png"));
		updateButton.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		updateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try {
					Customer customer = new CustomerModel();
					
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					customer = (Customer)setModel(composite, "cust", customer);
					
					customer = (Customer)session.get(CustomerModel.class, customer.getId());
					
					customer = (Customer)setModel(composite, "cust", customer);
					log.debug(customer.getClass().getSimpleName() + " : " + customer.getValues().toString());
					
					List<String> errors = CustomerAppNew.validateForm(customer);
					if (errors.isEmpty()) {
						customer = (Customer)preUpdate(customer, getParams());
						
						int rowCount = doUpdate(customer);
						
						if (rowCount == 1) {
							message(getShell(), "Update Success.", "Customer updated successfully.");
							
							getParams().put(AppConstant.APPCTRL, CustomerAppCtrl.CUST_CTRL);
							getParams().put(AppConstant.ACTION, CustomerAppCtrl.CUST_ENQ);
							getShell().close();
						}
					} else {
						message(getShell(), "Update Error.", errors, SWT.ICON_ERROR | SWT.OK);
					}
				} catch(Exception ex) {
					message(getShell(), "Update Error.", ex.getMessage(), SWT.ICON_ERROR | SWT.OK);
					log.error(ex.getMessage(), ex);
				}
			}
		});
		updateButton.setText("Update");
		updateButton.setBounds(151, 655, 90, 30);

		final Button resetButton = new Button(shell, SWT.NONE);
		resetButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				getShell().close();
			}
		});
		resetButton.setImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/Refresh.png"));
		resetButton.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		resetButton.setText("Reset");
		resetButton.setBounds(247, 655, 80, 30);

		text = new Text(composite, SWT.BORDER);
		text.setToolTipText("cust_name");
		text.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		text.setBounds(141, 28, 327, 30);

		text_1 = new Text(composite, SWT.BORDER);
		text_1.setEditable(false);
		text_1.setToolTipText("cust_code");
		text_1.setBounds(141, 64, 245, 30);
		text_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_2 = new Text(composite, SWT.BORDER);
		text_2.setToolTipText("cust_ic");
		text_2.setBounds(141, 100, 245, 30);
		text_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_3 = new Text(composite, SWT.BORDER);
		text_3.setToolTipText("cust_phone");
		text_3.setBounds(141, 136, 245, 30);
		text_3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_4 = new Text(composite, SWT.BORDER);
		text_4.setToolTipText("cust_email");
		text_4.setBounds(607, 107, 245, 30);
		text_4.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_5 = new Text(composite, SWT.BORDER);
		text_5.setToolTipText("cust_birthdate");
		text_5.setBounds(141, 244, 146, 30);
		text_5.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_6 = new Text(composite, SWT.BORDER);
		text_6.setToolTipText("cust_joindate");
		text_6.setBounds(141, 280, 146, 30);
		text_6.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_7 = new Text(composite, SWT.BORDER);
		text_7.setVisible(false);
		text_7.setForeground(SWTResourceManager.getColor(255, 0, 0));
		text_7.setToolTipText("cust_expirydate");
		text_7.setBounds(141, 316, 146, 30);
		text_7.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_6.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent arg0) {
				if (text_6.getText().length() > 0) {
					try {
						Date expiryDate = sdf.parse(text_6.getText());
						Calendar expiryCalendar = Calendar.getInstance();
						expiryCalendar.setTime(expiryDate);
						expiryCalendar.add(Calendar.YEAR, 1);
						text_7.setText(sdf.format(expiryCalendar.getTime()));
					} catch(Exception e) {
						log.error("Unable to set default expiry date.", e);
					}
				}
			}
		});
		
		text_8 = new Text(composite, SWT.BORDER);
		text_8.setVisible(false);
		text_8.setToolTipText("cust_discount");
		text_8.setBounds(607, 293, 146, 30);
		text_8.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_9 = new Text(composite, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
		text_9.setToolTipText("cust_address");
		text_9.setBounds(141, 352, 327, 101);
		text_9.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel.setText("Name");
		nameLabel.setBounds(10, 31, 45, 20);

		final Label nameLabel_1 = new Label(composite, SWT.NONE);
		nameLabel_1.setBounds(10, 67, 45, 20);
		nameLabel_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_1.setText("Code");

		final Label nameLabel_2 = new Label(composite, SWT.NONE);
		nameLabel_2.setBounds(10, 103, 85, 20);
		nameLabel_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_2.setText("IC/Passport");

		final Label nameLabel_3 = new Label(composite, SWT.NONE);
		nameLabel_3.setBounds(10, 139, 50, 20);
		nameLabel_3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_3.setText("Phone");

		final Label nameLabel_4 = new Label(composite, SWT.NONE);
		nameLabel_4.setBounds(482, 110, 45, 20);
		nameLabel_4.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_4.setText("Email");

		final Label nameLabel_5 = new Label(composite, SWT.NONE);
		nameLabel_5.setBounds(10, 247, 90, 20);
		nameLabel_5.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_5.setText("Date of Birth");

		final Label nameLabel_6 = new Label(composite, SWT.NONE);
		nameLabel_6.setBounds(10, 283, 70, 20);
		nameLabel_6.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_6.setText("Join Date");

		final Label nameLabel_7 = new Label(composite, SWT.NONE);
		nameLabel_7.setVisible(false);
		nameLabel_7.setForeground(SWTResourceManager.getColor(255, 0, 0));
		nameLabel_7.setBounds(10, 319, 80, 20);
		nameLabel_7.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_7.setText("Expiy Date");

		final Label nameLabel_8 = new Label(composite, SWT.NONE);
		nameLabel_8.setVisible(false);
		nameLabel_8.setBounds(482, 296, 65, 20);
		nameLabel_8.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_8.setText("Discount");

		final Label nameLabel_9 = new Label(composite, SWT.NONE);
		nameLabel_9.setBounds(10, 355, 60, 20);
		nameLabel_9.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_9.setText("Address");

		text_10 = new Text(composite, SWT.BORDER);
		text_10.setVisible(false);
		text_10.setToolTipText("cust_id");
		text_10.setBounds(607, 15, 80, 25);

		text_11 = new Text(composite, SWT.BORDER);
		text_11.setVisible(false);
		text_11.setToolTipText("cust_status");
		text_11.setBounds(607, 44, 80, 25);

		pickDateButton = new Button(composite, SWT.NONE);
		pickDateButton.setImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/Calendar.png"));
		pickDateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pickDate(getShell(), text_5);
			}
		});
		pickDateButton.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		pickDateButton.setBounds(295, 244, 30, 30);

		pickDateButton_1 = new Button(composite, SWT.NONE);
		pickDateButton_1.setImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/Calendar.png"));
		pickDateButton_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pickDate(getShell(), text_6);
			}
		});
		pickDateButton_1.setBounds(295, 280, 30, 30);
		pickDateButton_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));

		pickDateButton_2 = new Button(composite, SWT.NONE);
		pickDateButton_2.setVisible(false);
		pickDateButton_2.setImage(SWTResourceManager.getImage(CustomerAppMgm.class, "/img/Calendar.png"));
		pickDateButton_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pickDate(getShell(), text_7);
			}
		});
		pickDateButton_2.setBounds(295, 316, 30, 30);
		pickDateButton_2.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		
		final Label nameLabel_10 = new Label(composite, SWT.NONE);
		nameLabel_10.setVisible(false);
		nameLabel_10.setBounds(482, 260, 100, 20);
		nameLabel_10.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_10.setText("Receipt Code");

		text_13 = new Text(composite, SWT.BORDER);
		text_13.setVisible(false);
		text_13.setBounds(607, 257, 255, 30);
		text_13.setToolTipText("cust_rcpcode");
		text_13.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		
		list = new org.eclipse.swt.widgets.List(composite, SWT.BORDER);
		list.setVisible(false);
		list.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		list.setToolTipText("cust_joinmethod");
		list.setBounds(607, 146, 245, 75);
		list.add(Customer.JOIN_FOCP);
		list.add(Customer.JOIN_FOCE);
		list.add(Customer.JOIN_RM10);
		
		final Label nameLabel_10_1 = new Label(composite, SWT.NONE);
		nameLabel_10_1.setVisible(false);
		nameLabel_10_1.setBounds(482, 150, 100, 20);
		nameLabel_10_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		nameLabel_10_1.setText("Join Method");

		final Label txtLabel_10 = new Label(composite, SWT.NONE);
		txtLabel_10.setBounds(482, 73, 55, 20);
		txtLabel_10.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_10.setText("Gender");

		combo_1 = new Combo(composite, SWT.NONE);
		combo_1.setBounds(607, 70, 228, 28);
		combo_1.setToolTipText("cust_gender");
		combo_1.select(0);
		combo_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		combo_1.add(Customer.FEMALE);
		combo_1.add(Customer.MALE);

		final Label txtLabel_2_1 = new Label(composite, SWT.NONE);
		txtLabel_2_1.setBounds(10, 178, 95, 20);
		txtLabel_2_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_2_1.setText("Office Phone");

		text_14 = new Text(composite, SWT.BORDER);
		text_14.setBounds(141, 172, 259, 30);
		text_14.setToolTipText("cust_ophone");
		text_14.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		text_15 = new Text(composite, SWT.BORDER);
		text_15.setBounds(141, 208, 259, 30);
		text_15.setToolTipText("cust_hphone");
		text_15.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Label txtLabel_2_2 = new Label(composite, SWT.NONE);
		txtLabel_2_2.setBounds(10, 211, 90, 20);
		txtLabel_2_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_2_2.setText("Hand Phone");

		final Label txtLabel_12 = new Label(composite, SWT.NONE);
		txtLabel_12.setBounds(10, 462, 45, 20);
		txtLabel_12.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_12.setText("State");

		combo_2 = new Combo(composite, SWT.NONE);
		combo_2.setBounds(141, 459, 340, 28);
		combo_2.setToolTipText("cust_state");
		combo_2.select(0);
		combo_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		combo_2.add(Customer.STATE_0);
		combo_2.add(Customer.STATE_1);
		combo_2.add(Customer.STATE_2);
		combo_2.add(Customer.STATE_3);
		combo_2.add(Customer.STATE_4);
		combo_2.add(Customer.STATE_5);
		combo_2.add(Customer.STATE_6);
		combo_2.add(Customer.STATE_7);
		combo_2.add(Customer.STATE_8);
		combo_2.add(Customer.STATE_9);
		combo_2.add(Customer.STATE_10);
		combo_2.add(Customer.STATE_11);
		combo_2.add(Customer.STATE_12);
		combo_2.add(Customer.STATE_13);
		combo_2.add(Customer.STATE_14);

		final Label txtLabel_13 = new Label(composite, SWT.NONE);
		txtLabel_13.setBounds(10, 496, 70, 20);
		txtLabel_13.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_13.setText("Postcode");

		text_16 = new Text(composite, SWT.BORDER);
		text_16.setBounds(141, 493, 165, 30);
		text_16.setToolTipText("cust_postcode");
		text_16.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));

		final Label txtLabel_14 = new Label(composite, SWT.NONE);
		txtLabel_14.setBounds(10, 532, 55, 20);
		txtLabel_14.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		txtLabel_14.setText("Country");

		combo_3 = new Combo(composite, SWT.NONE);
		combo_3.setBounds(141, 529, 259, 28);
		combo_3.setToolTipText("cust_country");
		combo_3.select(0);
		combo_3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		combo_3.add(Customer.COUNTRY_0);
		combo_3.add(Customer.COUNTRY_1);
		combo_3.add(Customer.COUNTRY_2);
		//
		buildStandardMenu(getShell(), getParams(), CustomerAppCtrl.CUST_CTRL, CustomerAppCtrl.CUST_ENQ);
		populateControls(composite);
		
		final Label messageLabel = new Label(composite, SWT.NONE);
		messageLabel.setBounds(10, 2, 872, 20);
		messageLabel.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
		messageLabel.setText("Please Update the Customer Form below and click Update to save the changes");
		composite.setTabList(new Control[] {text, text_1, text_2, text_3, text_14, text_15, text_5, text_6, text_7, text_9, combo_2, text_16, combo_3, combo_1, text_4, list, text_13, text_8, text_10, text_11, pickDateButton, pickDateButton_1, pickDateButton_2});
	}
	
	private void populateControls(Composite composite) {
		if (getParams().get(CustomerAppCtrl.CUST_ID) == null) {
			getParams().put(CustomerAppCtrl.CUST_MSG, "Unable to display customer, customer ID not found.");
			
			getParams().put(AppConstant.APPCTRL, CustomerAppCtrl.CUST_CTRL);
			getParams().put(AppConstant.ACTION, CustomerAppCtrl.CUST_ENQ);
			getShell().close();
		}
		
		try {
			Long id = (Long)getParams().get(CustomerAppCtrl.CUST_ID);
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			
			Customer customer = (Customer)session.get(CustomerModel.class, id);
			if (customer != null) {
				setControls(composite, "cust", customer);
			}
		} catch(Exception e) {
			getParams().put(CustomerAppCtrl.CUST_MSG, e.getMessage());
			log.error(e.getMessage(), e);
			
			getParams().put(AppConstant.APPCTRL, CustomerAppCtrl.CUST_CTRL);
			getParams().put(AppConstant.ACTION, CustomerAppCtrl.CUST_ENQ);
			getShell().close();
		}
	}
	
	private int doUpdate(Customer customer) throws Exception {
		int rowCount = 0;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			
			session.saveOrUpdate(customer);
			log.debug(customer.getClass().getSimpleName() + " : " + customer.getValues().toString());
			rowCount++;
			
			session.getTransaction().commit();
		} catch(Exception e) {
			session.getTransaction().rollback();
			log.error(e.getMessage(), e);
			throw e;
		}
		return rowCount;
	}
	
	public Shell getShell() {
		return shell;
	}

	public void setShell(Shell shell) {
		this.shell = shell;
	}

}
